#!/bin/bash

# 修复后的Docker启动脚本
echo "=== 启动Docker容器 ==="

# 检查Docker服务
if ! systemctl is-active --quiet docker; then
    echo "Docker服务未运行，正在启动..."
    sudo systemctl start docker
    sleep 3
fi

# 检查Docker是否可用
if ! docker info > /dev/null 2>&1; then
    echo "Docker不可用，请检查Docker服务"
    exit 1
fi

# 设置X11转发权限
echo "设置X11转发权限..."
xhost +local:docker > /dev/null 2>&1

# 设备检测
DEPTH_PATH=$(readlink -f /dev/astradepth 2>/dev/null || echo "")
UVC_PATH=$(readlink -f /dev/astrauvc 2>/dev/null || echo "")

echo "检测到的设备:"
echo "  深度相机: ${DEPTH_PATH:-未检测到}"
echo "  UVC相机: ${UVC_PATH:-未检测到}"

# 检查主机yahboomcar_ros2_ws目录是否存在且非空
YAHBOOM_HOST_DIR="$HOME/yahboomcar_ros2_ws"
MOUNT_YAHBOOM=false

if [ -d "$YAHBOOM_HOST_DIR" ] && [ "$(ls -A "$YAHBOOM_HOST_DIR" 2>/dev/null)" ]; then
    echo "检测到主机yahboomcar_ros2_ws目录存在且非空，将进行挂载"
    MOUNT_YAHBOOM=true
else
    echo "主机yahboomcar_ros2_ws目录不存在或为空，使用容器内原有目录"
    MOUNT_YAHBOOM=false
fi

# 确保必要目录存在
if [ "$MOUNT_YAHBOOM" = true ]; then
    mkdir -p "$HOME/yahboomcar_ros2_ws/"
fi
mkdir -p "$HOME/muto_xzt/"
mkdir -p "$HOME/muto-llm/"

# 构建Docker参数
DOCKER_ARGS=(
    "docker" "run" "-it"
    "--net=host"
    "--env=DISPLAY=$DISPLAY"
    "--env=QT_X11_NO_MITSHM=1"
    "--env=XDG_RUNTIME_DIR=/tmp"
    "--env=XDG_SESSION_TYPE=x11"
    "-v" "/tmp/.X11-unix:/tmp/.X11-unix:rw"
)

# 添加X11权限文件挂载（如果存在）
if [ -f "$HOME/.Xauthority" ]; then
    DOCKER_ARGS+=("-v" "$HOME/.Xauthority:/root/.Xauthority:rw")
    echo "添加X11权限文件挂载"
fi

# 条件性添加yahboomcar_ros2_ws挂载
if [ "$MOUNT_YAHBOOM" = true ]; then
    DOCKER_ARGS+=("-v" "$HOME/yahboomcar_ros2_ws/:/root/yahboomcar_ros2_ws/")
fi

DOCKER_ARGS+=(
    "-v" "$HOME/muto_xzt/:/root/muto_xzt/"
    "-v" "$HOME/muto-llm/:/root/muto-llm/"
    "-e" "OMP_NUM_THREADS=$(nproc)"
    "-e" "TORCH_NUM_THREADS=$(nproc)"
    "-p" "9090:9090"
    "-p" "8888:8888"
    "-e" "RMW_IMPLEMENTATION=rmw_cyclonedds_cpp"
)

# 添加可选文件挂载
if [ -f "$HOME/fastrtps_unicast.xml" ]; then
    DOCKER_ARGS+=("-v" "$HOME/fastrtps_unicast.xml:/root/fastrtps_unicast.xml")
fi

if [ -e "/dev/myserial" ]; then
    DOCKER_ARGS+=("--device=/dev/myserial")
    echo "  添加设备: /dev/myserial"
else
    echo "  未挂载设备: /dev/myserial，跳过"
fi

if [ -e "/dev/myspeech" ]; then
    DOCKER_ARGS+=("--device=/dev/myspeech")
    echo "  添加设备: /dev/myspeech"
else
    echo "  未挂载设备: /dev/myspeech，跳过"
fi

if [ -e "/dev/snd" ]; then
    DOCKER_ARGS+=("--device=/dev/snd")
    echo "  添加设备: /dev/snd"
else
    echo "  未挂载设备: /dev/snd，跳过"
fi

if [ -e "/dev/rplidar" ]; then
    DOCKER_ARGS+=("--device=/dev/rplidar")
    echo "  添加设备: /dev/rplidar"
else
    echo "  未挂载设备: /dev/rplidar，跳过"
fi

if [ -d "/dev/input" ]; then
    DOCKER_ARGS+=("--device=/dev/input")
    echo "  添加设备: /dev/input"
else
    echo "  未挂载设备: /dev/input，跳过"
fi

# 添加相机设备
if [ -n "$DEPTH_PATH" ] && [ -e "$DEPTH_PATH" ]; then
    DOCKER_ARGS+=("--device=$DEPTH_PATH")
    echo "  添加深度相机: $DEPTH_PATH"
fi

if [ -n "$UVC_PATH" ] && [ -e "$UVC_PATH" ]; then
    DOCKER_ARGS+=("--device=$UVC_PATH")
    echo "  添加UVC相机: $UVC_PATH"
fi

# 添加镜像和命令
DOCKER_ARGS+=("yahboomtechnology/ros-foxy-muto:2.9.20" "/bin/bash" "-c")

# 构建初始化脚本
INIT_SCRIPT=""
if [ -n "$DEPTH_PATH" ]; then
    INIT_SCRIPT="$INIT_SCRIPT ln -sf $DEPTH_PATH /dev/astradepth;"
fi
if [ -n "$UVC_PATH" ]; then
    INIT_SCRIPT="$INIT_SCRIPT ln -sf $UVC_PATH /dev/astrauvc;"
fi
INIT_SCRIPT="$INIT_SCRIPT cd ~; /bin/bash"

DOCKER_ARGS+=("$INIT_SCRIPT")

echo ""
echo "启动Docker容器..."
echo "镜像: yahboomtechnology/ros-foxy-muto:2.9.20"
echo "X11转发已配置"
echo ""

# 执行Docker命令
"${DOCKER_ARGS[@]}"

# 清理X11权限（可选）
# echo "清理X11转发权限..."
# xhost -local:docker > /dev/null 2>&1
